﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ResourceList.aspx.cs" Inherits="Atomic.Web.Pages.Sys.ResourceList" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" language="javascript">
        var editType;  //编辑模式：update, add
        $(function () {
            $('#resourceList').treegrid({
                url: '/Handlers/Sys/ResourceHandler.ashx/?method=GetResourceList',
                idField: 'ID',
                treeField: 'Name',
                frozenColumns: [[
	                { field: 'Name', title: '页面名称', width: 250 }
				]],
                columns: [[
                    { field: 'Key', title: '唯一键', width: 150 },
                    { field: 'Enabled', title: '启用', width: 50,
                        formatter: function (v) {
                            if (v) {
                                return '是';
                            } else {
                                return '<span style="color:#f00;">否<span>';
                            }
                        }
                    },
                    { field: 'Sort', title: '顺序', width: 50 },
                    { field: 'Url', title: '地址/路径', width: 250 },
                    { field: 'Cls', title: '样式', width: 150 }
                ]],
                toolbar: [
                    {
                        type: 'menubutton',
                        text: '新建',
                        iconCls: 'icon-add',
                        menu: '#tbCreate'
                    },
                    {
                        text: '编辑',
                        iconCls: 'icon-edit',
                        handler: function () {
                            editType = 'update';    //标记为更新模式
                            var selectedNode = $('#resourceList').treegrid('getSelected');
                            if (selectedNode == null) {
                                $.messager.alert('提示信息', '请选择要编辑的项', 'info');
                                return;
                            } else {
                                var id = selectedNode.ID;
                                var pid = '';
                                var parentNode = $('#resourceList').treegrid('getParent', id);
                                if (parentNode != null) {
                                    pid = parentNode.ID;
                                }
                                $('#resourceEditor').window('open');
                                $('#resourceEditor').window('refresh', 'ResourceEditor.aspx?PID=' + pid + '&ID=' + id);
                            }
                        }
                    },
                    {
                        text: '删除',
                        iconCls: 'icon-remove',
                        handler: function () {
                            var selectedNode = $('#resourceList').treegrid('getSelected');
                            if (selectedNode == null) {
                                $.messager.alert('提示信息', '请选择要删除的项', 'info');
                                return;
                            } else {
                                $.messager.confirm('提示信息', '确定要删除选中项？', function (b) {
                                    if (b) {
                                        //节点基本信息
                                        $.post('/Handlers/Sys/ResourceHandler.ashx/?method=RemoveResource',
                                        {
                                            ID: selectedNode.ID
                                        },
                                        function (data) {
                                            if (data.Success) {

                                            } else {
                                                $.messager.alert('提示信息', data.Message, 'info');
                                            }
                                        },
                                        'json');
                                    }
                                });
                            }
                        }
                    }
                ]
            });

            $('#tbCreate').menu({
                onClick: function (item) {
                    var pid = '';
                    switch (item.id) {
                        case 'itemRoot':
                            {
                                $('#resourceList').treegrid('unselectAll'); //取消所有选中
                                break;
                            }
                        case 'itemChild':
                            {
                                var selectedNode = $('#resourceList').treegrid('getSelected');
                                if (selectedNode == null) {
                                    $.messager.alert('提示信息', '请从下面列表中选中一项作为父节点', 'info');
                                    return;
                                } else {
                                    pid = selectedNode.ID;
                                }
                                break;
                            }
                        default:
                    }
                    editType = 'add';    //标记为新建模式
                    //打开编辑框
                    $('#resourceEditor').window('open');
                    $('#resourceEditor').window('refresh', 'ResourceEditor.aspx?PID=' + pid);
                }
            });

            //初始化资源编辑窗口对象
            $('<div id="resourceEditor"></div>').appendTo('body').window({
                href: 'javascript:void(0);',
                title: '新建资源',
                width: 380,
                top: $(window).height() * 0.2,
                closed: true
            });
        });
        //编辑框回调事件实现
        function OnResourceEditSuccess() {
            $('#resourceEditor').window('close');
            var node = $('#resourceList').treegrid('getSelected');
            //确定重新加载本节点还是父节点
            if (editType == 'update') {
                //更新本节点结果处理
                node = $('#resourceList').treegrid('getParent', node.ID);
                if (node != null) {
                    node = $('#resourceList').treegrid('reload', node.ID);
                }
            } else if (node == null) {
                //新建根节点结果处理
                $('#resourceList').treegrid('reload');
            } else {
                //新建子节点结果处理
                $('#resourceList').treegrid('reload', node.ID);
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div id="tbCreate" style="width: 100px;">
        <div id="itemRoot">
            根节点</div>
        <div id="itemChild">
            子节点</div>
    </div>
    <div id="resourceList">
    </div>
    </form>
</body>
</html>
